﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeamPrasioliteWarGame.Models;

namespace WarGameAppGame.Admin
{
    public partial class ManageBuildingTypes : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // The return type can be changed to IEnumerable, however to support
        // paging and sorting, the following parameters must be added:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<Building> GridViewAllBuildingTypes_GetData()
        {
            Entities db = new Entities();
            return db.Buildings.OrderBy(b => b.Type);
        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewAllBuildingTypes_UpdateItem(int id)
        {
            Entities db = new Entities();
            Building item = db.Buildings.SingleOrDefault(b => b.Id == id);
            // Load the item here, e.g. item = MyDataLayer.Find(id);
            if (item == null)
            {
                // The item wasn't found
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
                return;
            }
            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                // Save changes here, e.g. MyDataLayer.SaveChanges();
                db.SaveChanges();
            }
        }

        // The id parameter name should match the DataKeyNames value set on the control
        public void GridViewAllBuildingTypes_DeleteItem(int id)
        {
            Entities db = new Entities();
            var buildingToRemove = db.Buildings.Include("UserBuildings").SingleOrDefault(u => u.Id == id);
            var buildingRelationsToRemove = buildingToRemove.UserBuildings.ToList();
            for (int i = 0; i < buildingRelationsToRemove.Count; i++)
            {
                db.UserBuildings.Remove(buildingRelationsToRemove[i]);
            }

            db.Buildings.Remove(buildingToRemove);
            db.SaveChanges();
        }

        protected void ShowInsert_Click(object sender, EventArgs e)
        {
            this.ShowInsert.Visible = false;
            this.InsertPanel.Visible = true;
        }

        protected void CreateButton_Command(object sender, CommandEventArgs e)
        {
            Entities db = new Entities();
            using (db)
            {
                var newBuilding = new Building
                {
                    Type = this.NewBuildingType.Text,
                    WoodCost = int.Parse(this.NewBuildingWoodCost.Text),
                    IronCost = int.Parse(this.NewBuildingIronCost.Text),
                    BuildTimeInSeconds = int.Parse(this.NewBuildingBuildTime.Text)
                };

                db.Buildings.Add(newBuilding);
                db.SaveChanges();
            }

            this.GridViewAllBuildingTypes.DataBind();
            this.CancelButton_Command(sender, e);
        }

        protected void CancelButton_Command(object sender, CommandEventArgs e)
        {
            this.ShowInsert.Visible = true;
            this.InsertPanel.Visible = false;
        }
    }
}